﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace Walson.Form.Common
{

        
        /// <summary>
        /// 时钟和计时器
        /// </summary>
        [DefaultEvent("ValueInput")]
        public partial class SegSevenDisplay : Control
        {
            private string _BackString = string.Empty;
            private string _BackPeriodString = string.Empty;
            private string _ForeString = string.Empty;
            private string _ForePeriodString = string.Empty;
            private Pen _Pen;
            private SolidBrush _BackStringBrush = new SolidBrush(Color.FromArgb(32, 255, 255, 255));
            private SolidBrush _ForeStringBrush = new SolidBrush(Color.White);
            private SolidBrush _PanelBrush = new SolidBrush(Color.FromArgb(16, 16, 49));
            private bool _CanInput = false;

            private int _Contrast=32;
            /// <summary>
            /// 对比度
            /// </summary>
            public int Contrast
            {
                get
                {
                    return _Contrast;
                }
                set
                {
                    _Contrast = value;
                    _BackStringBrush = new SolidBrush(Color.FromArgb(_Contrast, _ShadowColor));
                    this.Invalidate();
                }
            }

            private int _DigitCount = 1;
            /// <summary>
            /// 数字位数
            /// </summary>
            public int DigitCount
            {
                get
                {
                    return _DigitCount;
                }
                set
                {
                    _DigitCount = value;
                    SetString();
                    this.Invalidate();
                }
            }

            private string _Value = "0.";
            /// <summary>
            /// 值
            /// </summary>
            public string Value
            {
                get
                {
                    return _Value;
                }
                set
                {
                    if (value.Length > DigitCount)
                        _Value = value.Substring(0, DigitCount);
                    else
                        _Value = value;
                    SetString();
                    this.Invalidate();
                }
            }

            private float _FontSize = 75;
            /// <summary>
            /// 字体大小
            /// </summary>
            public float FontSize
            {
                get
                {
                    return _FontSize;
                }
                set
                {
                    _FontSize = value;
                    this.Invalidate();
                }
            }

            private bool _HexMod = false;
            /// <summary>
            /// 是否是十六进制模式
            /// </summary>
            private bool HexMod
            {
                get
                {
                    return _HexMod;
                }
                set
                {
                    _HexMod = value;
                    SetString();
                    this.Invalidate();
                }
            }


            private bool _TrimZeros= true;
            /// <summary>
            /// 是否除0
            /// </summary>
            private bool TrimZeros
            {
                get
                {
                    return _TrimZeros;
                }
                set
                {
                    _TrimZeros = value;
                    SetString();
                    this.Invalidate();
                }
            }


            /// <summary>
            /// 前景色
            /// </summary>
            public override Color ForeColor
            {
                get
                {
                    return base.ForeColor;
                }
                set
                {
                    base.ForeColor = value;
                    _Pen = new Pen(value, 2);
                    _ForeStringBrush = new SolidBrush(value);
                    this.Invalidate();
                }

            }

            /// <summary>
            /// 背景色
            /// </summary>
            public override Color BackColor
            {
                get
                {
                    return base.BackColor;
                }
                set
                {
                    base.BackColor = value;
                    //_BackStringBrush = new SolidBrush(Color.FromArgb(_Contrast, value));
                    this.Invalidate();
                }
            }

            private Color _PanelColor = Color.FromArgb(16, 16, 49);
            /// <summary>
            /// 内部背景色
            /// </summary>
            public Color PanelColor
            {
                get
                {
                    return _PanelColor;
                }
                set
                {
                    _PanelColor = value;
                    _PanelBrush = new SolidBrush(value);
                    this.Invalidate();
                }
            }

            private Color _ShadowColor = Color.FromArgb(255,255,255);
            /// <summary>
            /// 未点亮数字的颜色
            /// </summary>
            public Color ShadowColor
            {
                get
                {
                    return _ShadowColor;
                }
                set
                {
                    _ShadowColor = value;
                    _BackStringBrush = new SolidBrush(Color.FromArgb(Contrast, value));
                    this.Invalidate();
                }
            }

            private StringAlignment _Alignment = StringAlignment.Near;
            /// <summary>
            /// 文字对齐方式
            /// </summary>
            public StringAlignment Alignment
            {
                get
                {
                    return _Alignment;
                }
                set
                {
                    _Alignment = value;
                    this.Invalidate();
                }
            }

            /// <summary>
            /// 指定文本字符串的对齐方式
            /// </summary>
            public enum StringAlignment
            {
                Near=0,
                Far=2,
            }

            /// <summary>
            /// 是否在输入模式
            /// </summary>
            public bool InputMode
            {
                private set;
                get;
            }

            private void SetString()
            {
                StringBuilder sbBackString = new StringBuilder();
                StringBuilder sbPeriodString = new StringBuilder();
                StringBuilder sbBackPeriodString = new StringBuilder();
                _ForeString = _Value;

                for (int i = 0; i < _DigitCount; i++)
                {
                    sbBackString.Append("8");
                }

                _BackString = sbBackString.ToString();
            }

            private void SetString0()
            {
                StringBuilder sbBackString = new StringBuilder();
                StringBuilder sbPeriodString = new StringBuilder();
                StringBuilder sbBackPeriodString = new StringBuilder();
                _ForeString = _Value.Replace(".", "");

                for (int i = 0; i < _DigitCount; i++)
                {
                    sbBackString.Append("8");
                    sbBackPeriodString.Append(".");
                }

                for (int i = 0; i < _Value.Length; i++)
                {
                    if (_Value[i] != '.')
                    {
                        sbPeriodString.Append(" ");
                    }
                    else
                    {
                        if (sbPeriodString.Length > 0)
                        {
                            sbPeriodString.Remove(sbPeriodString.Length - 1, 1);
                        }
                        sbPeriodString.Append(".");
                    }
                }

                _BackString = sbBackString.ToString();
                _ForePeriodString = sbPeriodString.ToString();
                _BackPeriodString = sbBackPeriodString.ToString();
            }


            /// <summary>
            /// LED 数码管显示器
            /// </summary>
            public SegSevenDisplay()
            {
                InitializeComponent();

                _Pen = new Pen(Color.White, 2f);
                base.BackColor = Color.FromArgb(55, 55, 63);
                base.ForeColor = Color.White;
                PanelColor = Color.FromArgb(16, 16, 49);
                this.DoubleBuffered = true;
                SetString();

                this.Click += delegate
                {
                    _CanInput = true;
                    this.InputMode = true;
                    this.Focus();
                };

                this.Leave += delegate
                {
                    _CanInput = false;
                    this.InputMode = false;
                };

                this.KeyPress += new KeyPressEventHandler(SegSevenDisplay_KeyPress);
                this.LostFocus += delegate
                {
                    _CanInput = false;
                    this.InputMode = false;
                };
                //this.PreviewKeyDown += new PreviewKeyDownEventHandler(SegSevenDisplay_PreviewKeyDown);
            }

            void SegSevenDisplay_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (_CanInput == false)
                {
                    e.Handled = true;
                    return;
                }

                if (_HexMod)
                {
                    if((e.KeyChar >= 'a' && e.KeyChar <= 'f') || (e.KeyChar >= 'A' && e.KeyChar <= 'F'))
                        this.Value += e.KeyChar;
                }

                if ((e.KeyChar >= '0' && e.KeyChar <= '9'))
                {
                    this.Value += e.KeyChar;
                }
                else if (e.KeyChar == '.')
                {
                    if (this.Value.Contains(".") == false && this.Value != string.Empty)
                        this.Value += e.KeyChar;
                }
                else if (e.KeyChar == '-')
                {
                    if (this.Value == string.Empty)
                        this.Value += e.KeyChar;
                }
                else if (e.KeyChar == (char)Keys.Back)
                {
                    if (this.Value != string.Empty)
                        this.Value = this.Value.Substring(0, this.Value.Length - 1);
                }
                onValueInput();
            }

            

            Font font;
            /// <summary>
            /// 绘制控件
            /// </summary>
            /// <param name="e">参数</param>
            protected override void OnPaint(PaintEventArgs e)
            {
                int min = Math.Min(this.Width, this.Height);
                int max = Math.Max(this.Width, this.Height);
                int r = -min / 2 + 30;
                float scaleFactorX = (float)this.Width / (float)max;
                float scaleFactorY = (float)this.Height / (float)max;
                SizeF size1,size2,size3,size;
                //Font font = new Font("Terminal", 12);
                using (Bitmap bmp = new Bitmap(this.Width, this.Height))
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                        g.Clear(base.BackColor);


                        PointF[] line = new PointF[2];



                        g.TranslateTransform(this.Width / 2f, this.Height / 2f);

                        StringFormat sf = new StringFormat();
                        sf.Alignment = (System.Drawing.StringAlignment)_Alignment;
                        sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
                        font = new Font(new Walson.Drawing.ResourceFont().GetEmbeddedFont(Assembly.GetExecutingAssembly().GetName().Name.ToString(), "SegNumbers.ttf"), _FontSize);
                        size1 = g.MeasureString(_BackString, font, new Point(0, 0), sf);
                        size2 = g.MeasureString(_ForeString, font, new Point(0, 0), sf);
                        if (size1.Width > size2.Width)
                            size = size1;
                        else
                            size = size2;
                        //size = size3;

              


                        PointF[] region = new PointF[5];

                        region[0] = new PointF(-size.Width / 2f - 5f, -size.Height / 2f );
                        region[1] = new PointF(size.Width / 2f + 5f, -size.Height / 2f );
                        region[2] = new PointF(size.Width / 2f + 5f, size.Height / 2f );
                        region[3] = new PointF(-size.Width / 2f - 5f, size.Height / 2f);
                        region[4] = new PointF(-size.Width / 2f - 5f, -size.Height / 2f);
                        g.FillPolygon(_PanelBrush, region);
                        g.DrawLine(Pens.Black, region[0], region[1]);
                        g.DrawLine(Pens.White, region[1], region[2]);
                        g.DrawLine(Pens.White, region[2], region[3]);
                        g.DrawLine(Pens.Black, region[3], region[4]);



                        RectangleF stringRect = new RectangleF(-size.Width / 2f, -size.Height / 2f, size.Width, size.Height);
                        
                        //g.DrawRectangle(Pens.Yellow, new Rectangle((int)stringRect.X, (int)stringRect.Y, (int)stringRect.Width, (int)stringRect.Height));
                        // 绘制数字
                        g.DrawString(_BackString, font, _BackStringBrush, stringRect, sf);
                        //g.DrawString("8888888888888888", font, _BackStringBrush, stringRect, sf);
                         //g.DrawString(_BackPeriodString, font, _BackStringBrush, stringRect, sf);
                         //g.DrawString("................", font, _BackStringBrush, stringRect, sf);
                        g.DrawString(_ForeString, font, _ForeStringBrush, stringRect, sf);
                        //g.DrawString(".           ", font, _ForeStringBrush, stringRect, sf);
                        //g.DrawString(_ForePeriodString, font, _ForeStringBrush, stringRect, sf);
                         //g.DrawString(".         ", font, _ForeStringBrush, stringRect, sf);
                         
                    }
                    e.Graphics.DrawImage(bmp, 0, 0);
                }
            }

            /// <summary>
            /// 值输入的事件处理方法
            /// </summary>
            /// <param name="sender">触发事件的控件</param>
            public delegate void ValueInputHandler(object sender);
            /// <summary>
            /// 值输入事件
            /// </summary>
            public event ValueInputHandler ValueInput;
            /// <summary>
            /// 值输入
            /// </summary>
            protected void onValueInput()
            {
                if (this.ValueInput != null)
                    this.ValueInput(this);
            }
        }

    
}
